home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.athene.co.uk!not-for-mail
- From: "C.J. Scaife" <JOLTSWIFT@Athene.co.uk>
- Subject: Re: Help! Newbie question ...
- Message-ID: <314FDB55.3717@Athene.co.uk>
- Date: Wed, 20 Mar 1996 10:17:57 +0000
- References: <4ilkfu$41q@Kaos.deepcove.com>
- Organization: JoltSwift Ltd.
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
- MIME-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- Sean Affleck wrote:
- >
- > Is this legal ?
- >
- > class A {...};
- >
- > A f();
- >
- > main() {
- > A a = f();
- > }
- >
- > A f()
- > {
- > A fa;
- > return fa;
- > }
- >
- > Can you create an object on the stack within a function and then return
- > the object ? I know that objects created on the stack are deleted at block or
- > function exit, so is it ok to return them and use the value to initialize
- > another object?
- > I realize that there are ways to get around this such as dynamically
- > allocating the object and returning a pointer or passing the function an
- > object which the function can initialize in whatever way it wants. However,
- > I'd like to know if this can be done 'cus it seems more tidy since the
- > returned object is automaticly deleted on block exit and can therefore be
- > ignored if not needed. I'd also like to know for my own personal interest.
- >
- > Thanks!
- >
- > Sean Affleck
- > nbennett@fraser.sfu.ca
-
- It's legal, but inefficient because the local object will be copied to
- the result space by the return statement. A better way is to construct
- it in the return statement. An example of this can be found in my
- which.cpp program documented at
- http://www.athene.co.uk/Joltswift/csact003.htm (or get the source code
- from CS_ACT.ZIP at that URL.
-
- Hope this helps :)
-
-